const {
    EditorShared
} = require('./editor-shared');

const {
    EditorType
} = require('./config');
const {
    Pasteboard
} = require('./pasteboard');

class EditorManager {

    static className() {
        return 'EditorManager';
    }

    constructor() {
        if (window.location.href.endsWith('/play')) {
            this._editorType = EditorType.MusicScorePlayer;
        } else if (window.location.href.endsWith('/performance-teaching')) {
            this._editorType = EditorType.PerformanceTeaching;
        } else if (window.location.href.endsWith('/music-appreciation')) {
            this._editorType = EditorType.MusicAppreciation;
        } else {
            this._editorType = EditorType.MusicScoreEditor;
        }
    }

    static sharedInstance() {
        if (!shared.editorManager) shared.editorManager = new this();
        return shared.editorManager;
    }

    get editorType() {
        return this._editorType;
    }

    set editorType(value) {
        this._editorType = value;
        this.currentMusicScoreData = this.currentEditor && this.currentEditorData.musicScoreData;
    }

    get pasteboard() {
        if (!this._pasteboard) {
            this._pasteboard = new Pasteboard();
        }
        return this._pasteboard;
    }

    set pasteboard(value) {
        this._pasteboard = value;
    }

    get currentEditorShared() {
        if (this.editorType === EditorType.MusicScoreEditor) {
            return this.editorSharedForMusicScoreEditor;
        } else if (this.editorType === EditorType.MusicScorePlayer) {
            return this.editorSharedForMusicScorePlayer;
        } else if (this.editorType === EditorType.MusicEducation) {
            return this.editorSharedForMusicEducation;
        } else if (this.editorType === EditorType.PerformanceTeaching) {
            return this.editorSharedForPerformanceTeaching;
        } else if (this.editorType === EditorType.MusicAppreciation) {
            return this.editorSharedForMusicAppreciation;
        }else if (this.editorType === EditorType.MusicReourcesCenter) {
            return this.editorSharedForMusicResourcesCenter;
        } else if(this.editorType === EditorType.WindInstruments){
            return this.editorSharedForMusicWindInstruments;
        }
        return null;
    }

    get currentEditor() {
        return this.currentEditorShared.editor;
    }

    get currentMusicScoreEditor() {
        return this.currentEditor.musicScoreEditor;
    }

    get currentEditorData() {
        return this.currentEditor.data;
    }

    get currentMusicScoreData() {
        return this._currentMusicScoreData;
    }

    set currentMusicScoreData(value) {
        this._currentMusicScoreData = value;
    }

    get currentSketchpadData() {
        return this.currentEditor.data.sketchpadData;
    }

    get currentSelectedObjectInfo() {
        return this.currentEditor.currentSelected.currentSelectedObjectInfo;
    }

    //右键元素的内容，当前是右键的哪一项 位置调整
    get currentEditorRightKey() {
        return this._currentEditorRightKey;
    }

    set currentEditorRightKey(value) {
        this._currentEditorRightKey = value;
    }


    editorSharedWithEditorType(editorType) {
        if (editorType === EditorType.MusicScoreEditor) {
            return this.editorSharedForMusicScoreEditor;
        } else if (editorType === EditorType.MusicScorePlayer) {
            return this.editorSharedForMusicScorePlayer;
        } else if (editorType === EditorType.MusicEducation) {
            return this.editorSharedForMusicEducation;
        } else if (editorType === EditorType.PerformanceTeaching) {
            return this.editorSharedForPerformanceTeaching;
        } else if (editorType === EditorType.MusicAppreciation) {
            return this.editorSharedForMusicAppreciation;
        } else if (editorType === EditorType.MusicReourcesCenter) {
            return this.editorSharedForMusicResourcesCenter;
        } else if(editorType === EditorType.WindInstruments){
            return this.editorSharedForMusicWindInstruments;
        }
        return null;
    }

    get editorSharedForMusicScoreEditor() {
        if (!this._editorSharedForMusicScoreEditor) {
            this._editorSharedForMusicScoreEditor = new EditorShared();
        }
        return this._editorSharedForMusicScoreEditor;
    }

    get editorSharedForMusicScorePlayer() {
        if (!this._editorSharedForMusicScorePlayer) {
            this._editorSharedForMusicScorePlayer = new EditorShared();
        }
        return this._editorSharedForMusicScorePlayer;
    }

    get editorSharedForMusicEducation() {
        if (!this._editorSharedForMusicEducation) {
            this._editorSharedForMusicEducation = new EditorShared();
        }
        return this._editorSharedForMusicEducation;
    }

    get editorSharedForPerformanceTeaching() {
        if (!this._editorSharedForPerformanceTeaching) {
            this._editorSharedForPerformanceTeaching = new EditorShared();
        }
        return this._editorSharedForPerformanceTeaching;
    }


    get editorSharedForMusicAppreciation() {
        if (!this._editorSharedForMusicAppreciation) {
            this._editorSharedForMusicAppreciation = new EditorShared();
        }
        return this._editorSharedForMusicAppreciation;
    }

    set editorSharedForMusicAppreciation(value) {
        this._editorSharedForMusicAppreciation = value;
    }

    get editorSharedForMusicResourcesCenter() {
        if (!this._editorSharedForMusicResourcesCenter) {
            this._editorSharedForMusicResourcesCenter = new EditorShared();
        }
        return this._editorSharedForMusicResourcesCenter;
    }

    set editorSharedForMusicResourcesCenter(value) {
        this._editorSharedForMusicResourcesCenter = value;
    }

    get editorSharedForMusicWindInstruments() {
        if (!this._editorSharedForMusicWindInstruments) {
            this._editorSharedForMusicWindInstruments = new EditorShared();
        }
        return this._editorSharedForMusicWindInstruments;
    }

    set editorSharedForMusicWindInstruments(value) {
        this._editorSharedForMusicWindInstruments = value;
    }

    initialCanvasWhenNeed() {
        if (!this.canvas) {
            this.canvas = document.createElement("canvas");
            this.canvas.width = 80 * 2;
            this.canvas.height = 45 * 2;
            this.ctx = this.canvas.getContext("2d");
        }
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }

    /**
     *直接svg转png base64
     */
    getPNGBase64WithSVGString(svgString) {
        this.initialCanvasWhenNeed();
        svgString = svgString.replace(/&nbsp;/g, ' ');
        this.ctx.drawSvg(svgString, 0, 0, 200000, 200000);
        return this.canvas.toDataURL("image/png");
    }

    getStringWidthWithString(string, style = {}) {
        this.initialCanvasWhenNeed();
        let {
            fontSize = 16,
        } = style;
        this.ctx.font = `${fontSize}px`;
        let text = this.ctx.measureText(string);
        this.ctx.fillText(string, 50, 200);
        return text.width;
    }

    get getAppVUEComponent() {
        return this._getAppVUEComponent;
    }

    set getAppVUEComponent(value) {
        this._getAppVUEComponent = value;
    }

    get getEditorVUEComponent() {
        return this._getEditorVUEComponent;
    }

    set getEditorVUEComponent(value) {
        this._getEditorVUEComponent = value;
    }

    // 范唱伴奏的播放状态
    get isPlaying() {
        return this._isPlaying;
    }

    set isPlaying(value) {
        this._isPlaying = value;
    }
}

module.exports = EditorManager.sharedInstance();
